package com.pirestupppai.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseAdapter;
import com.pirestupppai.base.BaseViewHolder;
import com.pirestupppai.bean.UserReplyTwoItem;
import com.pirestupppai.common.Constant;
import com.pirestupppai.ui.activity.DetailActivity;
import com.pirestupppai.utils.ImageLoadUtil;
import com.pirestupppai.utils.Utils;

import java.util.List;

/**
 * 个人中心作品
 *
 * author: xiaoluo
 * date: 2017/6/26 11:16
 */
public class UserReplyAdapter extends BaseAdapter<UserReplyTwoItem> {

    private Context mContext;
    private List<UserReplyTwoItem> mList;

    public UserReplyAdapter(Context context, List<UserReplyTwoItem> list) {
        super(list);
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_home_tag;
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        ImageView imageView1 = holder.getView(R.id.image_one);
        ImageView imageView2 = holder.getView(R.id.image_two);
        View lineView = holder.getView(R.id.line_view);
        TextView label1 = holder.getView(R.id.label_one);
        TextView label2 = holder.getView(R.id.label_two);

        label1.setVisibility(View.GONE);
        label2.setVisibility(View.GONE);

        final UserReplyTwoItem item = mList.get(position);
        if (item.getFeedItemList() == null || item.getFeedItemList().size() == 0) {
            return;
        }
        if (item.getFeedItemList().size() % 2 == 0) {
            lineView.setVisibility(View.VISIBLE);
            float ratio1 = item.getFeedItemList().get(0).getMedias().get(0).getImage_ratio();
            float ratio2 = item.getFeedItemList().get(1).getMedias().get(0).getImage_ratio();

            float height = (float) (Constant.WIDTH_OF_SCREEN - Utils.dpToPx(mContext, 5)) / (ratio1 + ratio2);
            float width1 = height * ratio1;
            float width2 = height * ratio2;

            RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) imageView1.getLayoutParams();
            params1.width = (int) width1;
            params1.height = (int) height;
            imageView1.setLayoutParams(params1);

            RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) imageView2.getLayoutParams();
            params2.width = (int) width2;
            params2.height = (int) height;
            imageView2.setLayoutParams(params2);

            RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams) lineView.getLayoutParams();
            params3.height = (int) height;
            lineView.setLayoutParams(params3);

            ImageLoadUtil.getInstance().loadImage(item.getFeedItemList().get(0).getMedias().get(0).getPathname(), imageView1, ImageLoadUtil.LoadOption.LOAD_MEDIUM);
            ImageLoadUtil.getInstance().loadImage(item.getFeedItemList().get(1).getMedias().get(0).getPathname(), imageView2, ImageLoadUtil.LoadOption.LOAD_MEDIUM);

            imageView1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, DetailActivity.class);
                    intent.putExtra(Constant.IntentKey.ID, item.getFeedItemList().get(0).getId());
                    mContext.startActivity(intent);
                }
            });
            imageView2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, DetailActivity.class);
                    intent.putExtra(Constant.IntentKey.ID, item.getFeedItemList().get(1).getId());
                    mContext.startActivity(intent);

                }
            });
        } else {
            lineView.setVisibility(View.GONE);
            RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) imageView1.getLayoutParams();
            params1.width = Constant.WIDTH_OF_SCREEN;
            params1.height = (int) ((float) Constant.WIDTH_OF_SCREEN / item.getFeedItemList().get(0).getMedias().get(0).getImage_ratio());
            imageView1.setLayoutParams(params1);

            RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams) lineView.getLayoutParams();
            params3.height = (int) ((float) Constant.WIDTH_OF_SCREEN / item.getFeedItemList().get(0).getMedias().get(0).getImage_ratio());
            lineView.setLayoutParams(params3);

            ImageLoadUtil.getInstance().loadImage(item.getFeedItemList().get(0).getMedias().get(0).getPathname(), imageView1, ImageLoadUtil.LoadOption.LOAD_MEDIUM);

            imageView1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, DetailActivity.class);
                    intent.putExtra(Constant.IntentKey.ID, item.getFeedItemList().get(0).getId());
                    mContext.startActivity(intent);
                }
            });
        }
    }
}